package com.cch.demo.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cch.demo.bus.domain.Goods;
import com.cch.demo.bus.domain.Inport;
import com.cch.demo.bus.domain.Provider;
import com.cch.demo.bus.service.GoodsService;
import com.cch.demo.bus.service.InportService;
import com.cch.demo.bus.service.ProviderService;
import com.cch.demo.bus.vo.GoodsVo;
import com.cch.demo.bus.vo.InportVo;
import com.cch.demo.sys.domain.User;
import com.cch.demo.sys.util.AppFileUtils;
import com.cch.demo.sys.util.DataGridView;
import com.cch.demo.sys.util.ResultObj;
import com.cch.demo.sys.util.WebUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 华哥
 * @since 2020-03-12
 */
@RestController
@RequestMapping("/inport")
public class InportController {

    @Resource
    private InportService inportServiceImpl;

    @Resource
    private ProviderService providerServiceImpl;

    @Resource
    private GoodsService goodsServiceImpl;

    @RequestMapping("/loadAllInport")
    public DataGridView loadAllInport(InportVo inportVo){
        QueryWrapper<Inport> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(inportVo.getProviderid()!=null&&inportVo.getProviderid()!=0,"providerid",inportVo.getProviderid());
        queryWrapper.eq(inportVo.getGoodsid()!=null&&inportVo.getGoodsid()!=0,"goodsid",inportVo.getGoodsid());
        queryWrapper.ge(inportVo.getStartTime()!=null, "inporttime", inportVo.getStartTime());
        queryWrapper.le(inportVo.getEndTime()!=null, "inporttime", inportVo.getEndTime());
        queryWrapper.like(StringUtils.isNotBlank(inportVo.getOperateperson()), "operateperson", inportVo.getOperateperson());
        queryWrapper.like(StringUtils.isNotBlank(inportVo.getRemark()), "remark", inportVo.getRemark());
        queryWrapper.orderByDesc("inporttime");

        IPage<Inport> page = new Page<>(inportVo.getPage(),inportVo.getLimit());
        inportServiceImpl.page(inportVo.getDataSource(), page,queryWrapper);

        //给inport中供应商添加名字
        List<Inport> inports  = page.getRecords();
//        for(Inport inport:inports){
//
//            Provider provider =  providerServiceImpl.getById(inport.getProviderid());
//            if(null!=provider){
//                inport.setProvidername(provider.getProvidername());
//            }
//
//            Goods goods = goodsServiceImpl.getById(inport.getGoodsid());
//            if(null!=goods){
//                inport.setGoodsname(goods.getGoodsname());
//                inport.setSize(goods.getSize());
//            }
//        }
        return new DataGridView(page.getTotal(),inports);
    }

    @RequestMapping("addInport")
    public ResultObj addInport(InportVo inportVo){
        try{

            inportVo.setInporttime(new Date());
            User user=(User) WebUtils.getSession().getAttribute("user");
            inportVo.setOperateperson(user.getName());
            inportServiceImpl.save(inportVo);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 修改
     */
    @RequestMapping("updateInport")
    public ResultObj updateInport(InportVo inportVo) {
        try {
            inportServiceImpl.updateById(inportVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除
     */
    @RequestMapping("deleteInport")
    public ResultObj deleteInport(Integer id) {
        try {
            inportServiceImpl.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

}

